package com.example.wifimonitor.com.hanj;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author hanj.cn@outlook.com
 */
public class MyUtils {

    private static String stok = "%7Bf%3Ep%7D*t%2BH%7DH%242(%5BGID%24Kh%5Ea~FhRt)j%24V";

    public static ArrayList<JSONObject> getMercuryResponse() {

        // 获取stok
        Scanner scanner = new Scanner(System.in);

        // url
        String url = "http://melogin.cn/stok=" + stok + "/ds";

        // 构造body
        String bodyStr = "{\"hosts_info\":{" + "\"table\":\"host_info\"" + "}," + "\"method\":\"get\"" + "}";

        // 发送获取设备信息的get请求
        JSONObject respJson = JSONUtil.parseObj(HttpUtil.post(url, bodyStr));

        // 判断stok是否成功获取
        if ("-40401".equals(respJson.get("error_code", String.class))) {

            // 没有获取到,需要输入stok
            System.out.print("stok已失效,请输入:");

            stok = scanner.nextLine();

            return null;
        }

        JSONArray respJsonArray = respJson.get("hosts_info", JSONObject.class).get("host_info", JSONArray.class);

        ArrayList<JSONObject> hostList = new ArrayList<>();

        // 解析json
        for (int i = 0; i < respJsonArray.size(); i++) {

            hostList.add(respJsonArray.get(i, JSONObject.class).get("host_info_" + i, JSONObject.class));

        }

        return hostList;
    }

    public static void handleHostNames(ArrayList<String> hostList) {

        // todo 应修改为从配置文件中读取
        for (User user : Judge.getUsers()) {
            Judge.isOnline(user, hostList);
        }

    }

    public static String getTime() {

        // 设置时区
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));

        // 获取格式化后的时间
        return new SimpleDateFormat("MM月dd日 HH时mm分", Locale.CHINA).format(new Date());
    }

}



